package com.softcits.email.job;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.mail.EmailException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.softcits.email.controller.EmailController;
import com.softcits.email.model.MailInfo;
import com.softcits.email.service.EmailService;

public class EmailJob implements Job {

    private final static Logger LOGGER = LoggerFactory.getLogger(EmailJob.class);
    @Autowired
    private EmailService emailService;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Map dataMap = context.getJobDetail().getJobDataMap();
        String toAddress = (String) dataMap.get("toAddress");
        String subject = (String) dataMap.get("subject");
        String content = (String) dataMap.get("content");
        String[] toArr = null;
        if(toAddress != null) {
            toArr = toAddress.split(",");
        }

        MailInfo info = new MailInfo();
        info.setSubject(subject);
        info.setContent(content);
        info.setToAddress(Arrays.asList(toArr));
        try {
            this.emailService.sendEmail(info);
            LOGGER.debug("Email Sent successfully...");
        } catch (EmailException e) {
            e.printStackTrace();
        }

    }

}
